/*--------------------------------------------------------------------------------
	DESCRIPTION: Cleaning American National Election Studies (ANES)

--------------------------------------------------------------------------------*/

clear
use "$data_dir/raw/anes_timeseries_cdf_stata_20220916/anes_timeseries_cdf_stata_20220916.dta"

rename VCF0901b state_code
drop if state_code=="99"

**********************************
*************TRUST VALUES*********
**********************************

gen trust_fed_govt = 1 if inlist(VCF0604, 3, 4)
replace trust_fed_govt = 0 if VCF0604==1|VCF0604==2

gen trust_govt_index = VCF0656 if VCF0656!=999 & VCF0656!=.

gen trust_others = 1 if VCF0619==2|VCF9244==1|VCF9244==2
replace trust_others = 0 if VCF0619==1|VCF9244==3|VCF9244==4|VCF9244==5

******************************************
***********OTHER POLITICAL VALUES*********
******************************************

//Did Respondent Vote in the National Elections
gen voted = 1 if VCF0702==2
replace voted = 0 if VCF0702==1

//Vote for incumbent president
gen pres_vote = ""
replace pres_vote = "Democrat" if VCF0704==1
replace pres_vote = "Republican" if VCF0704==2
replace pres_vote = "Other" if VCF0704==3

gen incumbent_party = ""
replace incumbent_party = "Republican" if VCF0004==1952 | VCF0004==1954
replace incumbent_party = "Republican" if VCF0004==1956 | VCF0004==1958
replace incumbent_party = "Democrat"   if VCF0004==1960 | VCF0004==1962
replace incumbent_party = "Democrat"   if VCF0004==1964 | VCF0004==1966
replace incumbent_party = "Republican" if VCF0004==1968 | VCF0004==1970
replace incumbent_party = "Republican" if VCF0004==1972 | VCF0004==1974
replace incumbent_party = "Democrat"   if VCF0004==1976 | VCF0004==1978
replace incumbent_party = "Republican" if VCF0004==1980 | VCF0004==1982
replace incumbent_party = "Republican" if VCF0004==1984 | VCF0004==1986
replace incumbent_party = "Republican" if VCF0004==1988 | VCF0004==1990
replace incumbent_party = "Democrat"   if VCF0004==1992 | VCF0004==1994
replace incumbent_party = "Democrat"   if VCF0004==1996 | VCF0004==1998
replace incumbent_party = "Republican" if VCF0004==2000 | VCF0004==2002
replace incumbent_party = "Republican" if VCF0004==2004 | VCF0004==2006
replace incumbent_party = "Democrat"   if VCF0004==2008 | VCF0004==2010
replace incumbent_party = "Democrat"   if VCF0004==2012 | VCF0004==2014
replace incumbent_party = "Republican" if VCF0004==2016 | VCF0004==2018
replace incumbent_party = "Democrat"   if VCF0004==2020

************************************************
*************INDIVIDUAL CHARACTERISTICS*********
************************************************

rename VCF0004 survey_year

gen age = VCF0101 if VCF0101!=0
replace age = -99 if age==.

gen yob = survey_year-age if age!=-99
replace yob = -99 if yob==.

gen generation = ""
replace generation = "pre_lost_gen" if yob < 1883 & yob!=-99 & yob!=. 
replace generation = "lost_gen" if yob >= 1883 & yob <= 1900 
replace generation = "greatest_gen" if yob >= 1901 & yob <= 1927
replace generation = "silent_gen" if yob >= 1928 & yob <= 1945
replace generation = "baby_boomer" if yob >= 1946 & yob <= 1964
replace generation = "gen_x" if yob >= 1965 & yob <= 1980
replace generation = "millenial" if yob >= 1981 & yob <= 1996
replace generation = "gen_z" if yob >= 1997 & yob <= 2012

gen cohort = ""
forvalues i = 1850(10)2010 {
	replace cohort = "`i's" if yob>=`i' & yob<(`i'+10)
}

*sex
*			-99
*			1 male
*			2 female

gen sex = 1 if VCF0104==1
replace sex = 2 if VCF0104==2
replace sex = -99 if VCF0104==0|VCF0104==3

*religion
*			1   Do not belong to a denomination
*			2   Buddhist                
*			3   Jewish                  
*			4   Christian               
*			5   Muslim                  
*			6   Other 
*		  -99   Missing 

gen religion = .
replace religion = 3 if VCF0128==3
replace religion = 4 if VCF0128==1|VCF0128==2
replace religion = 6 if VCF0128==4
replace religion = -99 if VCF0128==.|VCF0128==0

*edu
*			1   Primary
*			2   Secondary
*			3   Tertiary
*			4   Other
*		  -99   Missing

gen edu = .
replace edu = 1 if VCF0110==1
replace edu = 2 if VCF0110==2|VCF0110==3
replace edu = 3 if VCF0110==4
replace edu = -99 if VCF0110==0

*occupation                                         
*			1 	Professional and managerial                   
*			2 	Clerical and sales workers                    
*			3 	Skilled, semi-skilled and service workers     
*			4 	Laborers, except farm                         
*			5 	Farmers, farm managers, farm laborers and foremen;                                         
*			6 	Homemakers (1972-1992: 7 IN VCF0116, 4 in
*		  -99 	NA; member of armed forces; no occupation and 

gen occupation = .
replace occupation = 1 if VCF0115==1
replace occupation = 2 if VCF0115==2
replace occupation = 3 if VCF0115==3
replace occupation = 4 if VCF0115==4
replace occupation = 5 if VCF0115==5
replace occupation = 6 if VCF0115==6
replace occupation = -99 if VCF0115==0|VCF0115==.

*employment_status
*			1   Employed
*			2   Unemployed
*			3   Other
*		  -99   Missing

gen employment_status = .
replace employment_status = 1 if VCF0118==1
replace employment_status = 2 if VCF0118==2
replace employment_status = 3 if inlist(VCF0118, 3, 4, 5)
replace employment_status = -99 if VCF0118==9|VCF0118==.

*income
*			1   Band 1
*			2   Band 2
*			3   Band 3
*			4   Band 4
*			5   Band 5
*		  -99   Missing

gen income = .
replace income = 1 if VCF0114==1
replace income = 2 if VCF0114==2
replace income = 3 if VCF0114==3
replace income = 4 if VCF0114==4
replace income = 5 if VCF0114==5
replace income = -99 if VCF0114==0|VCF0114==.

*party
*			1   democrat  
*			2   republican  
*			3   independent
*		   -99  Missing

gen party = .
replace party = 1 if VCF0303==1
replace party = 2 if VCF0303==3
replace party = 3 if VCF0303==2
replace party = -99 if VCF0303==0|VCF0303==.


//incumbent_party x party affiliation
gen incumbent_partisan = 1 if (party==1 & incumbent_party=="Democrat") | (party==2 & incumbent_party=="Republican")
replace incumbent_partisan = 0 if incumbent_partisan==. & party!=-99

*partisan
*			1   independent or apolitical  
*			2   leaning independent  
*			3   weak partisan
*			4   strong partisan
*		   -99  Missing

gen partisan = .
replace partisan = 1 if VCF0305==1
replace partisan = 2 if VCF0305==2
replace partisan = 3 if VCF0305==3
replace partisan = 4 if VCF0305==4
replace partisan = -99 if VCF0305==.|VCF0305==0

*marital
*			1   Married
*			2   Not Married
*		  -99   Missing

gen marital = 1 if VCF0147==1 
replace marital = 2 if inlist(VCF0147, 2, 3, 4, 5, 7, 8)
replace marital = -99 if marital==.

*urban
*			1   Urban
*			2   Rural
*		  -99   Missing

gen urban = 1 if VCF0111==1|VCF0111==2
replace urban = 2 if VCF0111==3
replace urban = -99 if VCF0111==0|VCF0111==.


**ethnicity
*			0 	Missing, pre-1966 data                
*			1 	White non-Hispanic                    
*			2 	Black non-Hispanic                    
*			3 	Hispanic                              
*			4 	Other or multiple races, non-Hispanic 
*			9 	Missing, DK/REF/NA

gen ethnicity = .
replace ethnicity = 1 if VCF0105b==1
replace ethnicity = 2 if VCF0105b==2
replace ethnicity = 3 if VCF0105b==3
replace ethnicity = 4 if VCF0105b==4
replace ethnicity = -99 if VCF0105b==0|VCF0105b==9

*birth
*			1	Born in USA
*			0	Born outside USA
*		  -99	Missing

gen birth = .
replace birth = 1 if VCF0142>100 & VCF0142<200
replace birth = 2 if VCF0142>=200 & VCF0142<997
replace birth = -99 if inlist(VCF0142, 997, 998, 999, 000)

*birth_state
gen birth_state = ""
replace birth_state = "AL"	if VCF0132==141	| VCF0133==141
replace birth_state = "AK"	if VCF0132==180	| VCF0133==180
replace birth_state = "AZ"	if VCF0132==161	| VCF0133==161
replace birth_state = "AR"	if VCF0132==142	| VCF0133==142
replace birth_state = "CA"	if VCF0132==171	| VCF0133==171
replace birth_state = "CO"	if VCF0132==162	| VCF0133==162
replace birth_state = "CT"	if VCF0132==101	| VCF0133==101
replace birth_state = "DE"	if VCF0132==111	| VCF0133==111
replace birth_state = "FL"	if VCF0132==143	| VCF0133==143
replace birth_state = "GA"	if VCF0132==144	| VCF0133==144
replace birth_state = "HI"	if VCF0132==181	| VCF0133==181
replace birth_state = "ID"	if VCF0132==163	| VCF0133==163
replace birth_state = "IL"	if VCF0132==121	| VCF0133==121
replace birth_state = "IN"	if VCF0132==122	| VCF0133==122
replace birth_state = "IA"	if VCF0132==131	| VCF0133==131
replace birth_state = "KS"	if VCF0132==132	| VCF0133==132
replace birth_state = "LA"	if VCF0132==145	| VCF0133==145
replace birth_state = "ME"	if VCF0132==102	| VCF0133==102
replace birth_state = "MA"	if VCF0132==103	| VCF0133==103
replace birth_state = "MI"	if VCF0132==123	| VCF0133==123
replace birth_state = "MN"	if VCF0132==133	| VCF0133==133
replace birth_state = "MS"	if VCF0132==146	| VCF0133==146
replace birth_state = "MO"	if VCF0132==134	| VCF0133==134
replace birth_state = "MT"	if VCF0132==164	| VCF0133==164
replace birth_state = "NE"	if VCF0132==135	| VCF0133==135
replace birth_state = "NV"	if VCF0132==165	| VCF0133==165
replace birth_state = "NH"	if VCF0132==104	| VCF0133==104
replace birth_state = "NJ"	if VCF0132==112	| VCF0133==112
replace birth_state = "NM"	if VCF0132==166	| VCF0133==166
replace birth_state = "NY"	if VCF0132==113	| VCF0133==113
replace birth_state = "NC"	if VCF0132==147	| VCF0133==147
replace birth_state = "ND"	if VCF0132==136	| VCF0133==136
replace birth_state = "OH"	if VCF0132==124	| VCF0133==124
replace birth_state = "OK"	if VCF0132==153	| VCF0133==153
replace birth_state = "OR"	if VCF0132==172	| VCF0133==172
replace birth_state = "PA"	if VCF0132==114	| VCF0133==114
replace birth_state = "RI"	if VCF0132==105	| VCF0133==105
replace birth_state = "SC"	if VCF0132==148	| VCF0133==148
replace birth_state = "SD"	if VCF0132==137	| VCF0133==137
replace birth_state = "TN"	if VCF0132==154	| VCF0133==154
replace birth_state = "TX"	if VCF0132==149	| VCF0133==149
replace birth_state = "UT"	if VCF0132==167	| VCF0133==167
replace birth_state = "VT"	if VCF0132==106	| VCF0133==106
replace birth_state = "VA"	if VCF0132==140	| VCF0133==140
replace birth_state = "WA"	if VCF0132==173	| VCF0133==173
replace birth_state = "WV"	if VCF0132==156	| VCF0133==156
replace birth_state = "WI"	if VCF0132==125	| VCF0133==125
replace birth_state = "WY"	if VCF0132==168	| VCF0133==168
replace birth_state = "KY or MD" if birth==1 & birth_state==""

keep state_code survey_year trust_fed_govt trust_govt_index trust_others voted pres_vote incumbent_party age yob generation cohort sex religion edu occupation employment_status income party incumbent_partisan partisan marital urban ethnicity birth_state

save "$data_dir/clean/anes_clean.dta", replace
